--- title: Anomaly Detection using Facebook Prophet keywords: fastai sidebar: home_sidebar nb_path: "nbs/prophet.ipynb" ---
{% raw %}
{% endraw %}

Importing the Libraries

{% raw %}
import pandas as pd
from pathlib import Path
import optuna
import numpy as np
import plotly.express as px
import re

from tqdm.notebook import tqdm
from matplotlib import pyplot as plt
from fbprophet import Prophet
from fbprophet.plot import add_changepoints_to_plot, plot_cross_validation_metric
from fbprophet.diagnostics import cross_validation, performance_metrics
from pathlib import Path
from pandas.api.types import is_numeric_dtype

import plotly.graph_objs as go
{% endraw %}

Processing the data

{% raw %}
df = pd.read_csv('../data/data.csv')
{% endraw %} {% raw %}
df['timestamp'] = pd.to_datetime(df['timestamp'])
{% endraw %} {% raw %}
df = df.set_index('timestamp')
{% endraw %} {% raw %}
df = df.resample('60min').mean()
{% endraw %} {% raw %}
df = df.reset_index()
end_date = '2014-06-20 00:00:00'
mask = (df.timestamp <= end_date)
mask2 = (df.timestamp >= '2014-05-31 00:00:00')
df_temp = df.loc[mask]
df_temp = df_temp.reset_index()
df_temp = df_temp.loc[mask2]
df_temp = df_temp.drop(columns = 'index')
df_temp
timestamp value
407 2014-05-31 00:00:00 35.452250
408 2014-05-31 01:00:00 35.955000
409 2014-05-31 02:00:00 36.084750
410 2014-05-31 03:00:00 37.243167
411 2014-05-31 04:00:00 36.480917
... ... ...
883 2014-06-19 20:00:00 34.491417
884 2014-06-19 21:00:00 34.209667
885 2014-06-19 22:00:00 36.815667
886 2014-06-19 23:00:00 36.457917
887 2014-06-20 00:00:00 36.679250

481 rows × 2 columns

{% endraw %} {% raw %}
df = df.reset_index()
df = df.drop(columns = 'index')
df
timestamp value
0 2014-05-14 01:00:00 49.703600
1 2014-05-14 02:00:00 38.210083
2 2014-05-14 03:00:00 40.346917
3 2014-05-14 04:00:00 35.579833
4 2014-05-14 05:00:00 32.913333
... ... ...
1500 2014-07-15 13:00:00 13.603500
1501 2014-07-15 14:00:00 13.587250
1502 2014-07-15 15:00:00 13.580917
1503 2014-07-15 16:00:00 13.638167
1504 2014-07-15 17:00:00 17.999500

1505 rows × 2 columns

{% endraw %}

Utility Functions

{% raw %}
def train_baseline_prophet_model(df_red):
    m = Prophet()
    m.fit(df_red)
    return m

def prophet_objective(trial, df_past, horizon):
    seasonality_mode = trial.suggest_categorical('seasonality_mode', ["additive", "multiplicative"]) #
    changepoint_prior_scale = trial.suggest_uniform('changepoint_prior_scale', 0.001,0.5)
    changepoint_range = trial.suggest_float('changepoint_range', 0.50,0.85)
    seasonality_prior_scale = trial.suggest_float('seasonality_prior_scale', 0.01,10, log=True)#
    growth = trial.suggest_categorical('growth', ["linear", "flat"])

    m = Prophet(
        growth=growth,
        seasonality_mode=seasonality_mode,
        changepoint_prior_scale=changepoint_prior_scale,
        changepoint_range=changepoint_range,
        seasonality_prior_scale=seasonality_prior_scale,
        uncertainty_samples=0
    )

    m.fit(df_past)

    #Using INITIAL days to train, build a forecast of HORIZON days long, every PERIOD days
    df_cv = cross_validation(m, horizon=horizon)
    df_p = performance_metrics(df_cv, rolling_window=1)
    print(df_p)
    mape = np.mean(df_p.mape)
    return mape

def train_optimal_prophet_model(df: pd.DataFrame):
    study = optuna.create_study(direction='minimize')
    study.optimize(
        lambda trial: prophet_objective(
            trial, df, '10day'
        ),
        n_trials=30
    )

    return study.best_params
{% endraw %} {% raw %}
df.columns = ['ds', 'y']
{% endraw %} {% raw %}
end_date = '2014-06-25 00:00:00'
mask = (df['ds'] <= end_date)
df_train = df.loc[mask]
df_test = df.loc[~mask]
{% endraw %} {% raw %}
df_train = df_train.reset_index()
{% endraw %} {% raw %}
df_test = df_test.reset_index()
df_test
index ds y
0 1008 2014-06-25 01:00:00 36.715417
1 1009 2014-06-25 02:00:00 35.994750
2 1010 2014-06-25 03:00:00 37.360083
3 1011 2014-06-25 04:00:00 36.540833
4 1012 2014-06-25 05:00:00 36.586667
... ... ... ...
492 1500 2014-07-15 13:00:00 13.603500
493 1501 2014-07-15 14:00:00 13.587250
494 1502 2014-07-15 15:00:00 13.580917
495 1503 2014-07-15 16:00:00 13.638167
496 1504 2014-07-15 17:00:00 17.999500

497 rows × 3 columns

{% endraw %} {% raw %}
m = train_baseline_prophet_model(df_train)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
{% endraw %} {% raw %}
preds = m.predict(df_test)
{% endraw %} {% raw %}
m.plot(preds)
{% endraw %} {% raw %}
fig = go.Figure()
# Create and style traces
fig.add_trace(go.Scatter(x=df_test['ds'], y=df_test['y'], name='Actual',))
fig.add_trace(go.Scatter(x=preds['ds'], y=preds['yhat'], name='Prediction',))
fig.add_trace(go.Scatter(x=df_train['ds'], y=df_train['y'], name='Training Actuals',))
fig.add_trace(go.Scatter(x=preds['ds'], y=preds['yhat_lower'], name='yhat_lower', fill='tozeroy', mode='lines'))
fig.add_trace(go.Scatter(x=preds['ds'], y=preds['yhat_upper'], name='yhat_upper',fill='tozeroy', mode='lines'))
fig.update_layout(
    title=f"model prediction vs actual",
    xaxis_title="Timestamps",
    yaxis_title=f"value",
    font=dict(
        family="Courier New, monospace",
        size=12,
        color="RebeccaPurple"
    ))
# fig.write_html(f"/content/plots/{name}-{metric}.html")
fig.show()
{% endraw %} {% raw %}
df_cv = cross_validation(m, horizon='5day')
df_p = performance_metrics(df_cv, rolling_window=1)
INFO:fbprophet:Making 9 forecasts with cutoffs between 2014-05-31 00:00:00 and 2014-06-20 00:00:00

{% endraw %} {% raw %}
df_p
horizon mse rmse mae mape mdape coverage
0 5 days 32.053026 5.661539 4.481786 0.120496 0.096578 0.339815
{% endraw %} {% raw %}
best_params = train_optimal_prophet_model(df_train)
best_params
[I 2020-11-30 17:38:53,424] A new study created in memory with name: no-name-d353fd58-1b3d-42f3-9b0a-8f73d62f9119
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
[I 2020-11-30 17:38:53,744] Trial 0 finished with value: 0.07968598317861428 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.07715189134740501, 'changepoint_range': 0.6583956708577887, 'seasonality_prior_scale': 0.021697086242173588, 'growth': 'flat'}. Best is trial 0 with value: 0.07968598317861428.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.770575  3.281855  2.806459  0.079686  0.072619
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
[I 2020-11-30 17:38:55,560] Trial 1 finished with value: 0.2398730906546436 and parameters: {'seasonality_mode': 'additive', 'changepoint_prior_scale': 0.2952912456541562, 'changepoint_range': 0.6526898490085573, 'seasonality_prior_scale': 0.10950375243233926, 'growth': 'linear'}. Best is trial 0 with value: 0.07968598317861428.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
  horizon        mse      rmse       mae      mape    mdape
0 10 days  76.771463  8.761933  8.455841  0.239873  0.24587
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
[I 2020-11-30 17:38:58,146] Trial 2 finished with value: 0.1593245227669654 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.4829268982246444, 'changepoint_range': 0.8469816957897975, 'seasonality_prior_scale': 6.833896974133268, 'growth': 'linear'}. Best is trial 0 with value: 0.07968598317861428.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  36.434556  6.036104  5.571262  0.159325  0.176875

[I 2020-11-30 17:38:58,354] Trial 3 finished with value: 0.07994870629749615 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.4488290372615602, 'changepoint_range': 0.8255178409278422, 'seasonality_prior_scale': 4.120290926141589, 'growth': 'flat'}. Best is trial 0 with value: 0.07968598317861428.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.830149  3.290919  2.816187  0.079949  0.073814

[I 2020-11-30 17:38:58,575] Trial 4 finished with value: 0.07949013003274255 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.46445660359124197, 'changepoint_range': 0.7378452367712143, 'seasonality_prior_scale': 0.016214132872601063, 'growth': 'flat'}. Best is trial 4 with value: 0.07949013003274255.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae     mape     mdape
0 10 days  10.727285  3.275253  2.799212  0.07949  0.072333

[I 2020-11-30 17:38:58,791] Trial 5 finished with value: 0.079829700137243 and parameters: {'seasonality_mode': 'additive', 'changepoint_prior_scale': 0.1961612026131081, 'changepoint_range': 0.5694218859455757, 'seasonality_prior_scale': 0.017412169349294845, 'growth': 'flat'}. Best is trial 4 with value: 0.07949013003274255.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
  horizon        mse     rmse       mae     mape     mdape
0 10 days  10.802921  3.28678  2.811798  0.07983  0.073239
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
[I 2020-11-30 17:39:01,250] Trial 6 finished with value: 0.18261725720258146 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.4570239785290354, 'changepoint_range': 0.5081637820605427, 'seasonality_prior_scale': 2.108376851883814, 'growth': 'linear'}. Best is trial 4 with value: 0.07949013003274255.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse    rmse       mae      mape     mdape
0 10 days  45.671922  6.7581  6.412472  0.182617  0.195676
[I 2020-11-30 17:39:01,524] Trial 7 finished with value: 0.07994874619640237 and parameters: {'seasonality_mode': 'additive', 'changepoint_prior_scale': 0.3138106969372627, 'changepoint_range': 0.6708410031325269, 'seasonality_prior_scale': 2.328195790199701, 'growth': 'flat'}. Best is trial 4 with value: 0.07949013003274255.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.830197  3.290927  2.816188  0.079949  0.073815
[I 2020-11-30 17:39:01,779] Trial 8 finished with value: 0.07994380120606921 and parameters: {'seasonality_mode': 'additive', 'changepoint_prior_scale': 0.27040778046445246, 'changepoint_range': 0.5165585186503591, 'seasonality_prior_scale': 0.08595015146300473, 'growth': 'flat'}. Best is trial 4 with value: 0.07949013003274255.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.829057  3.290753  2.816006  0.079944  0.073791
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
[I 2020-11-30 17:39:04,595] Trial 9 finished with value: 0.19800321174353547 and parameters: {'seasonality_mode': 'additive', 'changepoint_prior_scale': 0.359838114035191, 'changepoint_range': 0.541450539404859, 'seasonality_prior_scale': 0.17132185668296895, 'growth': 'linear'}. Best is trial 4 with value: 0.07949013003274255.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  52.882973  7.272068  6.962779  0.198003  0.207914
[I 2020-11-30 17:39:04,940] Trial 10 finished with value: 0.07994838981446524 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.0018779934045478341, 'changepoint_range': 0.7568589825242797, 'seasonality_prior_scale': 0.6222061725830436, 'growth': 'flat'}. Best is trial 4 with value: 0.07949013003274255.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.830076  3.290908  2.816175  0.079948  0.073812
[I 2020-11-30 17:39:05,280] Trial 11 finished with value: 0.07886655065922676 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.07938875904430329, 'changepoint_range': 0.7344797541372162, 'seasonality_prior_scale': 0.010098266434137335, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon       mse      rmse       mae      mape     mdape
0 10 days  10.59581  3.255121  2.776148  0.078867  0.071434
[I 2020-11-30 17:39:05,587] Trial 12 finished with value: 0.07930312120482307 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.1561693130701493, 'changepoint_range': 0.7492760359688708, 'seasonality_prior_scale': 0.013463880230482792, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.686895  3.269082  2.792294  0.079303  0.072065
[I 2020-11-30 17:39:05,879] Trial 13 finished with value: 0.07895472311127903 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.1331744725770119, 'changepoint_range': 0.7606011334473547, 'seasonality_prior_scale': 0.010605882318143755, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon       mse     rmse       mae      mape     mdape
0 10 days  10.61378  3.25788  2.779411  0.078955  0.071561
[I 2020-11-30 17:39:06,130] Trial 14 finished with value: 0.07988824408378807 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.0762852459046556, 'changepoint_range': 0.7918978308941856, 'seasonality_prior_scale': 0.04593012605700594, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse     rmse      mae      mape     mdape
0 10 days  10.816273  3.28881  2.81395  0.079888  0.073512
[I 2020-11-30 17:39:06,391] Trial 15 finished with value: 0.07994818344091513 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.11457095775754784, 'changepoint_range': 0.7236866390000923, 'seasonality_prior_scale': 0.4862659883262004, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.830029  3.290901  2.816167  0.079948  0.073811
[I 2020-11-30 17:39:06,631] Trial 16 finished with value: 0.07988440852458958 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.03086847292585522, 'changepoint_range': 0.7013159292042292, 'seasonality_prior_scale': 0.044508430852747305, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.815393  3.288677  2.813807  0.079884  0.073492
[I 2020-11-30 17:39:06,904] Trial 17 finished with value: 0.0798630689390922 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.20236645900973327, 'changepoint_range': 0.6114848751941533, 'seasonality_prior_scale': 0.03814639176175949, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.810542  3.287939  2.813012  0.079863  0.073379
[I 2020-11-30 17:39:07,173] Trial 18 finished with value: 0.07891502110633941 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.12679291867173303, 'changepoint_range': 0.7924161750506904, 'seasonality_prior_scale': 0.010369444970081257, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.605667  3.256634  2.777942  0.078915  0.071504
[I 2020-11-30 17:39:07,472] Trial 19 finished with value: 0.07994655851711913 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.040621100872866134, 'changepoint_range': 0.8285057600131519, 'seasonality_prior_scale': 0.24117423660461515, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.829656  3.290844  2.816107  0.079947  0.073803
[I 2020-11-30 17:39:07,714] Trial 20 finished with value: 0.07892303358847105 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.1707609793705331, 'changepoint_range': 0.8040804470966608, 'seasonality_prior_scale': 0.010416081554392227, 'growth': 'flat'}. Best is trial 11 with value: 0.07886655065922676.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.607302  3.256885  2.778238  0.078923  0.071515
[I 2020-11-30 17:39:07,965] Trial 21 finished with value: 0.07886199483220623 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.19582257984718568, 'changepoint_range': 0.80442050437026, 'seasonality_prior_scale': 0.010073652565524494, 'growth': 'flat'}. Best is trial 21 with value: 0.07886199483220623.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.594887  3.254979  2.775979  0.078862  0.071427
[I 2020-11-30 17:39:08,206] Trial 22 finished with value: 0.07979734483312241 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.09319233628245338, 'changepoint_range': 0.7829876140647438, 'seasonality_prior_scale': 0.02879887578194013, 'growth': 'flat'}. Best is trial 21 with value: 0.07886199483220623.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.795612  3.285668  2.810581  0.079797  0.073054

[I 2020-11-30 17:39:08,432] Trial 23 finished with value: 0.07907720900111313 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.21625281061071508, 'changepoint_range': 0.7844731960731589, 'seasonality_prior_scale': 0.011427493906743691, 'growth': 'flat'}. Best is trial 21 with value: 0.07886199483220623.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.639088  3.261762  2.783944  0.079077  0.071739

[I 2020-11-30 17:39:08,687] Trial 24 finished with value: 0.07992228430313733 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.24409925329582652, 'changepoint_range': 0.7139579241806719, 'seasonality_prior_scale': 0.06946894197401761, 'growth': 'flat'}. Best is trial 21 with value: 0.07886199483220623.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon       mse      rmse      mae      mape     mdape
0 10 days  10.82409  3.289998  2.81521  0.079922  0.073684

[I 2020-11-30 17:39:08,954] Trial 25 finished with value: 0.07973585350474653 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.15095133203095557, 'changepoint_range': 0.8425151251460291, 'seasonality_prior_scale': 0.024181172310405067, 'growth': 'flat'}. Best is trial 21 with value: 0.07886199483220623.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon       mse      rmse       mae      mape     mdape
0 10 days  10.78175  3.283558  2.808305  0.079736  0.072746

[I 2020-11-30 17:39:09,217] Trial 26 finished with value: 0.0788698177778658 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.028606793740209274, 'changepoint_range': 0.6958615714026617, 'seasonality_prior_scale': 0.010115981271426157, 'growth': 'flat'}. Best is trial 21 with value: 0.07886199483220623.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
  horizon        mse      rmse       mae     mape     mdape
0 10 days  10.596473  3.255222  2.776269  0.07887  0.071438
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
[I 2020-11-30 17:39:09,872] Trial 27 finished with value: 0.18538355959678704 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.014334308445309654, 'changepoint_range': 0.6323444713142696, 'seasonality_prior_scale': 0.0275671850081145, 'growth': 'linear'}. Best is trial 21 with value: 0.07886199483220623.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  47.101366  6.863043  6.511862  0.185384  0.195178
[I 2020-11-30 17:39:10,122] Trial 28 finished with value: 0.07990606170981386 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.05066620493800397, 'changepoint_range': 0.6943070396985469, 'seasonality_prior_scale': 0.0539319608494089, 'growth': 'flat'}. Best is trial 21 with value: 0.07886199483220623.
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
INFO:fbprophet:Making 1 forecasts with cutoffs between 2014-06-15 00:00:00 and 2014-06-15 00:00:00
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.820383  3.289435  2.814605  0.079906  0.073598
[I 2020-11-30 17:39:10,370] Trial 29 finished with value: 0.07956194423150302 and parameters: {'seasonality_mode': 'multiplicative', 'changepoint_prior_scale': 0.08032276774577753, 'changepoint_range': 0.6833006468412298, 'seasonality_prior_scale': 0.017740426235372697, 'growth': 'flat'}. Best is trial 21 with value: 0.07886199483220623.
  horizon        mse      rmse       mae      mape     mdape
0 10 days  10.743047  3.277659  2.801869  0.079562  0.072438
{'seasonality_mode': 'multiplicative',
 'changepoint_prior_scale': 0.19582257984718568,
 'changepoint_range': 0.80442050437026,
 'seasonality_prior_scale': 0.010073652565524494,
 'growth': 'flat'}
{% endraw %} {% raw %}
m = Prophet(
    growth=best_params['growth'],
    seasonality_mode=best_params['seasonality_mode'],
    changepoint_prior_scale=best_params['changepoint_prior_scale'],
    changepoint_range=best_params['changepoint_range'],
    seasonality_prior_scale=best_params['seasonality_prior_scale']
)
{% endraw %} {% raw %}
m.fit(df_train)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.
<fbprophet.forecaster.Prophet at 0x7fb73ba50160>
{% endraw %} {% raw %}
 
{% endraw %} {% raw %}
preds = m.predict(df_test)
{% endraw %} {% raw %}
m.plot(preds)
{% endraw %} {% raw %}
fig = go.Figure()
# Create and style traces
fig.add_trace(go.Scatter(x=df_test['ds'], y=df_test['y'], name='Actual',))
fig.add_trace(go.Scatter(x=preds['ds'], y=preds['yhat'], name='Prediction',))
fig.add_trace(go.Scatter(x=df_train['ds'], y=df_train['y'], name='Training Actuals',))
fig.add_trace(go.Scatter(x=preds['ds'], y=preds['yhat_lower'], name='yhat_lower', fill='tozeroy', mode='lines'))
fig.add_trace(go.Scatter(x=preds['ds'], y=preds['yhat_upper'], name='yhat_upper',fill='tozeroy', mode='lines'))
fig.update_layout(
    title=f"model prediction vs actual",
    xaxis_title="Timestamps",
    yaxis_title=f"value",
    font=dict(
        family="Courier New, monospace",
        size=12,
        color="RebeccaPurple"
    ))
fig.show()
{% endraw %} {% raw %}
df_cv = cross_validation(m, horizon='5day')
df_p = performance_metrics(df_cv)
INFO:fbprophet:Making 9 forecasts with cutoffs between 2014-05-31 00:00:00 and 2014-06-20 00:00:00

{% endraw %} {% raw %}
df_p.describe()
horizon mse rmse mae mape mdape coverage
count 109 109.000000 109.000000 109.000000 109.000000 109.000000 109.000000
mean 2 days 18:00:00 12.775630 3.507843 2.529577 0.066295 0.055390 0.807085
std 1 days 07:36:34.551714921 4.770325 0.689221 0.266074 0.005583 0.004708 0.066645
min 0 days 12:00:00 4.862897 2.205198 1.909002 0.053352 0.043088 0.657407
25% 1 days 15:00:00 9.403473 3.066508 2.371997 0.062546 0.052146 0.759259
50% 2 days 18:00:00 12.432554 3.525983 2.588886 0.066803 0.054661 0.796296
75% 3 days 21:00:00 15.589150 3.948310 2.711019 0.070187 0.059092 0.861111
max 5 days 00:00:00 20.981976 4.580609 2.943139 0.076586 0.064475 0.925926
{% endraw %} {% raw %}
m.plot(preds)
{% endraw %} {% raw %}
preds2 = m.predict(df_temp)
{% endraw %} {% raw %}
m.plot_components(preds)
{% endraw %}